<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
  Class: Mocha::Mock
  
    &mdash; Mocha 3.0.0-rc.2
  
</title>

  <link rel="stylesheet" href="../css/style.css" type="text/css" />

  <link rel="stylesheet" href="../css/common.css" type="text/css" />

<script type="text/javascript">
  pathId = "Mocha::Mock";
  relpath = '../';
</script>


  <script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>

  <script type="text/javascript" charset="utf-8" src="../js/app.js"></script>


  </head>
  <body>
    <div class="nav_wrap">
      <iframe id="nav" src="../class_list.html?1"></iframe>
      <div id="resizer"></div>
    </div>

    <div id="main" tabindex="-1">
      <div id="header">
        <div id="menu">
  
    <a href="../_index.html">Index (M)</a> &raquo;
    <span class='title'><span class='object_link'><a href="../Mocha.html" title="Mocha (module)">Mocha</a></span></span>
     &raquo; 
    <span class="title">Mock</span>
  
</div>

        <div id="search">
  
    <a class="full_list_link" id="class_list_link"
        href="../class_list.html">

        <svg width="24" height="24">
          <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
          <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
          <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
        </svg>
    </a>
  
</div>
        <div class="clear"></div>
      </div>

      <div id="content"><h1>Class: Mocha::Mock
  
  
  
</h1>
<div class="box_info">
  
  <dl>
    <dt>Inherits:</dt>
    <dd>
      <span class="inheritName"><span class='object_link'>Object</span></span>
      
        <ul class="fullTree">
          <li><span class='object_link'>Object</span></li>
          
            <li class="next">Mocha::Mock</li>
          
        </ul>
        <a href="#" class="inheritanceTree">show all</a>
      
    </dd>
  </dl>
  

  
  
  
  
  

  

  
  <dl>
    <dt>Defined in:</dt>
    <dd>lib/mocha/mock.rb</dd>
  </dl>
  
</div>

<h2>Overview</h2><div class="docstring">
  <div class="discussion">
    
<p>Traditional mock object.</p>

<p><span class='object_link'><a href="#expects-instance_method" title="Mocha::Mock#expects (method)">#expects</a></span> and <span class='object_link'><a href="#stubs-instance_method" title="Mocha::Mock#stubs (method)">#stubs</a></span> return an <span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span> which can be further modified by methods on <span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span>.</p>

<p><span class='object_link'><a href="#responds_like-instance_method" title="Mocha::Mock#responds_like (method)">#responds_like</a></span> and <span class='object_link'><a href="#responds_like_instance_of-instance_method" title="Mocha::Mock#responds_like_instance_of (method)">#responds_like_instance_of</a></span> both return a <span class='object_link'><a href="" title="Mocha::Mock (class)">Mock</a></span>, and can therefore, be chained to the original creation methods in <span class='object_link'><a href="API.html" title="Mocha::API (module)">API</a></span>. They force the mock to indicate what it is supposed to be mocking, thus making it a safer verifying mock. They check that the underlying <code>responder</code> will actually respond to the methods being stubbed, throwing a <code>NoMethodError</code> upon invocation otherwise.</p>

<p>Stubs and expectations are basically the same thing. A stub is just an expectation of zero or more invocations. The <span class='object_link'><a href="#stubs-instance_method" title="Mocha::Mock#stubs (method)">#stubs</a></span> method is syntactic sugar to make the intent of the test more explicit.</p>

<p>When a method is invoked on a mock object, the mock object searches through its expectations from newest to oldest to find one that matches the invocation. After the invocation, the matching expectation might stop matching further invocations. For example, an <tt>expects(:foo).once</tt> expectation only matches once and will be ignored on future invocations while an <tt>expects(:foo).at_least_once</tt> expectation will always be matched against invocations.</p>

<p>However, note that if the expectation that matches the invocation has a cardinality of “never”, then an unexpected invocation error is reported.</p>

<p>This scheme allows you to:</p>
<ul><li>
<p>Set up default stubs in your the <code>setup</code> method of your test class and override some of those stubs in individual tests.</p>
</li><li>
<p>Set up different <code>once</code> expectations for the same method with different action per invocation. However, it’s better to use the <span class='object_link'><a href="Expectation.html#returns-instance_method" title="Mocha::Expectation#returns (method)">Expectation#returns</a></span> method with multiple arguments to do this, as described below.</p>
</li></ul>

<p>However, there are some possible “gotchas” caused by this scheme:</p>
<ul><li>
<p>if you create an expectation and then a stub for the same method, the stub will always override the expectation and the expectation will never be met.</p>
</li><li>
<p>if you create a stub and then an expectation for the same method, the expectation will match, and when it stops matching the stub will be used instead, possibly masking test failures.</p>
</li><li>
<p>if you create different expectations for the same method, they will be invoked in the opposite order than that in which they were specified, rather than the same order.</p>
</li></ul>

<p>The best thing to do is not set up multiple expectations and stubs for the same method with exactly the same matchers. Instead, use the <span class='object_link'><a href="Expectation.html#returns-instance_method" title="Mocha::Expectation#returns (method)">Expectation#returns</a></span> method with multiple arguments to create multiple actions for a method. You can also chain multiple calls to <span class='object_link'><a href="Expectation.html#returns-instance_method" title="Mocha::Expectation#returns (method)">Expectation#returns</a></span> and <span class='object_link'><a href="Expectation.html#raises-instance_method" title="Mocha::Expectation#raises (method)">Expectation#raises</a></span> (along with syntactic sugar <span class='object_link'><a href="Expectation.html#then-instance_method" title="Mocha::Expectation#then (method)">Expectation#then</a></span> if desired).</p>

<p>If you want to specify more complex ordering or order invocations across different mock objects, use the <span class='object_link'><a href="Expectation.html#in_sequence-instance_method" title="Mocha::Expectation#in_sequence (method)">Expectation#in_sequence</a></span> method to explicitly define a total or partial ordering of invocations.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='period'>.</span><span class='id identifier rubyid_raises'>raises</span><span class='lparen'>(</span><span class='const'>Exception</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 1
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 2
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; raises exception of class Exception1</span></code></pre>
    
  </div>


</div>






  
    <h2>
      Instance Method Summary
      <small><a href="#" class="summary_toggle">collapse</a></small>
    </h2>

    <ul class="summary">
      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#expects-instance_method" title="#expects (instance method)">#<strong>expects</strong>(method_name_or_hash, backtrace = nil)  &#x21d2; Expectation </a>
    

    
      (also: #__expects__)
    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Adds an expectation that the specified method must be called exactly once with any parameters.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#responds_like-instance_method" title="#responds_like (instance method)">#<strong>responds_like</strong>(responder)  &#x21d2; Mock </a>
    

    
      (also: #quacks_like)
    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Constrains the <span class='object_link'><a href="" title="Mocha::Mock (class)">Mock</a></span> instance so that it can only expect or stub methods to which <code>responder</code> responds publicly.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#responds_like_instance_of-instance_method" title="#responds_like_instance_of (instance method)">#<strong>responds_like_instance_of</strong>(responder_class)  &#x21d2; Mock </a>
    

    
      (also: #quacks_like_instance_of)
    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Constrains the <span class='object_link'><a href="" title="Mocha::Mock (class)">Mock</a></span> instance so that it can only expect or stub methods to which an instance of the <code>responder_class</code> responds publicly.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#stubs-instance_method" title="#stubs (instance method)">#<strong>stubs</strong>(method_name_or_hash, backtrace = nil)  &#x21d2; Expectation </a>
    

    
      (also: #__stubs__)
    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Adds an expectation that the specified method may be called any number of times with any parameters.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#unstub-instance_method" title="#unstub (instance method)">#<strong>unstub</strong>(*method_names)  &#x21d2; Object </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Removes the specified stubbed methods (added by calls to <span class='object_link'><a href="#expects-instance_method" title="Mocha::Mock#expects (method)">#expects</a></span> or <span class='object_link'><a href="#stubs-instance_method" title="Mocha::Mock#stubs (method)">#stubs</a></span>) and all expectations associated with them.</p>
</div></span>
  
</li>

      
    </ul>
  

<div id="method_missing_details" class="method_details_list">
  <h2>Dynamic Method Handling</h2>
  <p class="notice this">
    This class handles dynamic methods through the <tt>method_missing</tt> method
    
  </p>
  
    <div class="method_details first">
  <h3 class="signature first" id="method_missing-instance_method">
  
    #<strong>method_missing</strong>(symbol, *arguments, &amp;block)  &#x21d2; <tt><span class='object_link'>Object</span></tt> 
  

  

  
</h3><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


319
320
321</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/mock.rb', line 319</span>

<span class='kw'>def</span> <span class='id identifier rubyid_method_missing'>method_missing</span><span class='lparen'>(</span><span class='id identifier rubyid_symbol'>symbol</span><span class='comma'>,</span> <span class='op'>*</span><span class='id identifier rubyid_arguments'>arguments</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_handle_method_call'>handle_method_call</span><span class='lparen'>(</span><span class='id identifier rubyid_symbol'>symbol</span><span class='comma'>,</span> <span class='id identifier rubyid_arguments'>arguments</span><span class='comma'>,</span> <span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
  
</div>


  <div id="instance_method_details" class="method_details_list">
    <h2>Instance Method Details</h2>

    
      <div class="method_details first">
  <h3 class="signature first" id="expects-instance_method">
  
    
      <span class="overload">#<strong>expects</strong>(method_name)  &#x21d2; <tt><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
      <span class="overload">#<strong>expects</strong>(expected_methods_vs_return_values)  &#x21d2; <tt><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
  

  
    <span class="aliases">Also known as:
    <span class="names"><span id='__expects__-instance_method'>__expects__</span></span>
    </span>
  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Adds an expectation that the specified method must be called exactly once with any parameters.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method invoked once so no error raised</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method not invoked so error raised</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span>
<span class='comment'># error raised when test completes, because expected_method not called exactly once</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method invoked twice so error raised</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; error raised when expected method invoked second time</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Setup multiple expectations using <code>expected_methods_vs_return_values</code>.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='label'>expected_method_one:</span> <span class='symbol'>:result_one</span><span class='comma'>,</span> <span class='label'>expected_method_two:</span> <span class='symbol'>:result_two</span><span class='rparen'>)</span>

<span class='comment'># is exactly equivalent to
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method_one</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='symbol'>:result_one</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method_two</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='symbol'>:result_two</span><span class='rparen'>)</span></code></pre>
    
  </div>

  <p class="tag_title">Overloads:</p>
  <ul class="overload">
    
      
      <li class="overload_item">
        <span class="signature">#<strong>expects</strong>(method_name)  &#x21d2; <tt><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
        <div class="docstring">
  <div class="discussion">
    

  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>method_name</span>
      
      
        <span class='type'>(<tt>Symbol</tt>, <tt>String</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>name of expected method</p>
</div>
      
    </li>
  
</ul>


</div>
      </li>
    
      
      <li class="overload_item">
        <span class="signature">#<strong>expects</strong>(expected_methods_vs_return_values)  &#x21d2; <tt><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
        <div class="docstring">
  <div class="discussion">
    

  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>expected_methods_vs_return_values</span>
      
      
        <span class='type'>(<tt>Hash</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>expected method name symbols as keys and corresponding return values as values - these expectations are setup as if <span class='object_link'><a href="#expects-instance_method" title="Mocha::Mock#expects (method)">#expects</a></span> were called multiple times.</p>
</div>
      
    </li>
  
</ul>


</div>
      </li>
    
  </ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>last-built expectation which can be further modified by methods on <span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span>.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


116
117
118
119
120
121
122
123
124
125
126
127</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/mock.rb', line 116</span>

<span class='kw'>def</span> <span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='id identifier rubyid_method_name_or_hash'>method_name_or_hash</span><span class='comma'>,</span> <span class='id identifier rubyid_backtrace'>backtrace</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_expectation'>expectation</span> <span class='op'>=</span> <span class='kw'>nil</span>
  <span class='id identifier rubyid_iterator'>iterator</span> <span class='op'>=</span> <span class='const'>ArgumentIterator</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_method_name_or_hash'>method_name_or_hash</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_iterator'>iterator</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_method_name'>method_name</span><span class='comma'>,</span> <span class='op'>*</span><span class='id identifier rubyid_args'>args</span><span class='op'>|</span>
    <span class='id identifier rubyid_ensure_method_not_already_defined'>ensure_method_not_already_defined</span><span class='lparen'>(</span><span class='id identifier rubyid_method_name'>method_name</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_expectation'>expectation</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='kw'>self</span><span class='comma'>,</span> <span class='id identifier rubyid_method_name'>method_name</span><span class='comma'>,</span> <span class='id identifier rubyid_backtrace'>backtrace</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_expectation'>expectation</span><span class='period'>.</span><span class='id identifier rubyid_in_sequence'>in_sequence</span><span class='lparen'>(</span><span class='ivar'>@mockery</span><span class='period'>.</span><span class='id identifier rubyid_sequences'>sequences</span><span class='period'>.</span><span class='id identifier rubyid_last'>last</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='ivar'>@mockery</span><span class='period'>.</span><span class='id identifier rubyid_sequences'>sequences</span><span class='period'>.</span><span class='id identifier rubyid_any?'>any?</span>
    <span class='id identifier rubyid_expectation'>expectation</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='id identifier rubyid_args'>args</span><span class='period'>.</span><span class='id identifier rubyid_shift'>shift</span><span class='rparen'>)</span> <span class='kw'>unless</span> <span class='id identifier rubyid_args'>args</span><span class='period'>.</span><span class='id identifier rubyid_empty?'>empty?</span>
    <span class='ivar'>@expectations</span><span class='period'>.</span><span class='id identifier rubyid_add'>add</span><span class='lparen'>(</span><span class='id identifier rubyid_expectation'>expectation</span><span class='rparen'>)</span>
  <span class='kw'>end</span>
  <span class='id identifier rubyid_expectation'>expectation</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="responds_like-instance_method">
  
    #<strong>responds_like</strong>(responder)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Mock (class)">Mock</a></span></tt> 
  

  
    <span class="aliases">Also known as:
    <span class="names"><span id='quacks_like-instance_method'>quacks_like</span></span>
    </span>
  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Constrains the <span class='object_link'><a href="" title="Mocha::Mock (class)">Mocha::Mock</a></span> instance so that it can only expect or stub methods to which <code>responder</code> responds publicly. The constraint is only applied at method invocation time.</p>

<p>A <code>NoMethodError</code> will be raised if the <code>responder</code> does not publicly <code>#respond_to?</code> the invoked method (even if the method has been expected or stubbed).</p>

<p>The <span class='object_link'><a href="" title="Mocha::Mock (class)">Mocha::Mock</a></span> instance will delegate its <code>#respond_to?</code> method to the <code>responder</code>. However, the <code>include_all</code> parameter is not passed through, so only public methods on the <code>responder</code> will be considered.</p>

<p>Note that the methods on <code>responder</code> are never actually invoked.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Normal mocking</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_sheep'>sheep</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>sheep</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:chew</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:foo</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='symbol'>:chew</span><span class='rparen'>)</span> <span class='comment'># =&gt; true
</span><span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='symbol'>:foo</span><span class='rparen'>)</span> <span class='comment'># =&gt; true
</span><span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_chew'>chew</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span>
<span class='comment'># no error raised</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Using <span class='object_link'><a href="#responds_like-instance_method" title="Mocha::Mock#responds_like (method)">#responds_like</a></span> with an instance method</p>
</div></h5>
      
      <pre class="example code"><code><span class='kw'>class</span> <span class='const'>Sheep</span>
  <span class='kw'>def</span> <span class='id identifier rubyid_chew'>chew</span><span class='lparen'>(</span><span class='id identifier rubyid_grass'>grass</span><span class='rparen'>)</span><span class='semicolon'>;</span> <span class='kw'>end</span>
<span class='kw'>end</span>

<span class='id identifier rubyid_sheep'>sheep</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>sheep</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_responds_like'>responds_like</span><span class='lparen'>(</span><span class='const'>Sheep</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:chew</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:foo</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='symbol'>:chew</span><span class='rparen'>)</span> <span class='comment'># =&gt; true
</span><span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='symbol'>:foo</span><span class='rparen'>)</span> <span class='comment'># =&gt; false
</span><span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_chew'>chew</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># =&gt; raises NoMethodError exception</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Using <span class='object_link'><a href="#responds_like-instance_method" title="Mocha::Mock#responds_like (method)">#responds_like</a></span> with a class method</p>
</div></h5>
      
      <pre class="example code"><code><span class='kw'>class</span> <span class='const'>Sheep</span>
  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_number_of_legs'>number_of_legs</span><span class='semicolon'>;</span> <span class='kw'>end</span>
<span class='kw'>end</span>

<span class='id identifier rubyid_sheep_class'>sheep_class</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>sheep_class</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep_class'>sheep_class</span><span class='period'>.</span><span class='id identifier rubyid_responds_like'>responds_like</span><span class='lparen'>(</span><span class='const'>Sheep</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep_class'>sheep_class</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:number_of_legs</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='int'>4</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep_class'>sheep_class</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:foo</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep_class'>sheep_class</span><span class='period'>.</span><span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='symbol'>:number_of_legs</span><span class='rparen'>)</span> <span class='comment'># =&gt; true
</span><span class='id identifier rubyid_sheep_class'>sheep_class</span><span class='period'>.</span><span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='symbol'>:foo</span><span class='rparen'>)</span> <span class='comment'># =&gt; false
</span><span class='id identifier rubyid_sheep_class'>sheep_class</span><span class='period'>.</span><span class='id identifier rubyid_number_of_legs'>number_of_legs</span> <span class='comment'># =&gt; 4
</span><span class='id identifier rubyid_sheep_class'>sheep_class</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># =&gt; raises NoMethodError exception</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>responder</span>
      
      
        <span class='type'>(<tt><span class='object_link'>Object</span></tt>, <tt>#respond_to?</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>an object used to determine whether <span class='object_link'><a href="" title="Mocha::Mock (class)">Mocha::Mock</a></span> instance should <code>#respond_to?</code> to an invocation.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Mock (class)">Mock</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same <span class='object_link'><a href="" title="Mocha::Mock (class)">Mocha::Mock</a></span> instance, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Mock (class)">Mocha::Mock</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

  <p class="tag_title">See Also:</p>
  <ul class="see">
    
      <li><span class='object_link'><a href="#responds_like_instance_of-instance_method" title="Mocha::Mock#responds_like_instance_of (method)">#responds_like_instance_of</a></span></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


241
242
243
244</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/mock.rb', line 241</span>

<span class='kw'>def</span> <span class='id identifier rubyid_responds_like'>responds_like</span><span class='lparen'>(</span><span class='id identifier rubyid_responder'>responder</span><span class='rparen'>)</span>
  <span class='ivar'>@responder</span> <span class='op'>=</span> <span class='id identifier rubyid_responder'>responder</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="responds_like_instance_of-instance_method">
  
    #<strong>responds_like_instance_of</strong>(responder_class)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Mock (class)">Mock</a></span></tt> 
  

  
    <span class="aliases">Also known as:
    <span class="names"><span id='quacks_like_instance_of-instance_method'>quacks_like_instance_of</span></span>
    </span>
  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Constrains the <span class='object_link'><a href="" title="Mocha::Mock (class)">Mocha::Mock</a></span> instance so that it can only expect or stub methods to which an instance of the <code>responder_class</code> responds publicly. The constraint is only applied at method invocation time. Note that the responder instance is instantiated using <tt>Class#allocate</tt>.</p>

<p>A <code>NoMethodError</code> will be raised if the responder instance does not publicly <code>#respond_to?</code> the invoked method (even if the method has been expected or stubbed).</p>

<p>The <span class='object_link'><a href="" title="Mocha::Mock (class)">Mocha::Mock</a></span> instance will delegate its <code>#respond_to?</code> method to the responder instance. However, the <code>include_all</code> parameter is not passed through, so only public methods on the <code>responder</code>  will be considered.</p>

<p>Note that the methods on the responder instance are never actually invoked.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Using <span class='object_link'><a href="#responds_like_instance_of-instance_method" title="Mocha::Mock#responds_like_instance_of (method)">#responds_like_instance_of</a></span></p>
</div></h5>
      
      <pre class="example code"><code><span class='kw'>class</span> <span class='const'>Sheep</span>
  <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span>
    <span class='id identifier rubyid_raise'>raise</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>some awkward code we don&#39;t want to call</span><span class='tstring_end'>&quot;</span></span>
  <span class='kw'>end</span>
  <span class='kw'>def</span> <span class='id identifier rubyid_chew'>chew</span><span class='lparen'>(</span><span class='id identifier rubyid_grass'>grass</span><span class='rparen'>)</span><span class='semicolon'>;</span> <span class='kw'>end</span>
<span class='kw'>end</span>

<span class='id identifier rubyid_sheep'>sheep</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>sheep</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_responds_like_instance_of'>responds_like_instance_of</span><span class='lparen'>(</span><span class='const'>Sheep</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:chew</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:foo</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='symbol'>:chew</span><span class='rparen'>)</span> <span class='comment'># =&gt; true
</span><span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='symbol'>:foo</span><span class='rparen'>)</span> <span class='comment'># =&gt; false
</span><span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_chew'>chew</span>
<span class='id identifier rubyid_sheep'>sheep</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># =&gt; raises NoMethodError exception</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>responder_class</span>
      
      
        <span class='type'>(<tt>Class</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>a class used to determine whether <span class='object_link'><a href="" title="Mocha::Mock (class)">Mocha::Mock</a></span> instance should <code>#respond_to?</code> to an invocation.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Mock (class)">Mock</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same <span class='object_link'><a href="" title="Mocha::Mock (class)">Mocha::Mock</a></span> instance, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Mock (class)">Mocha::Mock</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

  <p class="tag_title">See Also:</p>
  <ul class="see">
    
      <li><span class='object_link'><a href="#responds_like-instance_method" title="Mocha::Mock#responds_like (method)">#responds_like</a></span></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


274
275
276</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/mock.rb', line 274</span>

<span class='kw'>def</span> <span class='id identifier rubyid_responds_like_instance_of'>responds_like_instance_of</span><span class='lparen'>(</span><span class='id identifier rubyid_responder_class'>responder_class</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_responds_like'>responds_like</span><span class='lparen'>(</span><span class='id identifier rubyid_responder_class'>responder_class</span><span class='period'>.</span><span class='id identifier rubyid_allocate'>allocate</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="stubs-instance_method">
  
    
      <span class="overload">#<strong>stubs</strong>(method_name)  &#x21d2; <tt><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
      <span class="overload">#<strong>stubs</strong>(stubbed_methods_vs_return_values)  &#x21d2; <tt><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
  

  
    <span class="aliases">Also known as:
    <span class="names"><span id='__stubs__-instance_method'>__stubs__</span></span>
    </span>
  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Adds an expectation that the specified method may be called any number of times with any parameters.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>No error raised however many times stubbed method is invoked</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:stubbed_method</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubbed_method'>stubbed_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubbed_method'>stubbed_method</span>
<span class='comment'># no error raised</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Setup multiple expectations using <code>stubbed_methods_vs_return_values</code>.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='label'>stubbed_method_one:</span> <span class='symbol'>:result_one</span><span class='comma'>,</span> <span class='label'>stubbed_method_two:</span> <span class='symbol'>:result_two</span><span class='rparen'>)</span>

<span class='comment'># is exactly equivalent to
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:stubbed_method_one</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='symbol'>:result_one</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:stubbed_method_two</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='symbol'>:result_two</span><span class='rparen'>)</span></code></pre>
    
  </div>

  <p class="tag_title">Overloads:</p>
  <ul class="overload">
    
      
      <li class="overload_item">
        <span class="signature">#<strong>stubs</strong>(method_name)  &#x21d2; <tt><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
        <div class="docstring">
  <div class="discussion">
    

  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>method_name</span>
      
      
        <span class='type'>(<tt>Symbol</tt>, <tt>String</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>name of stubbed method</p>
</div>
      
    </li>
  
</ul>


</div>
      </li>
    
      
      <li class="overload_item">
        <span class="signature">#<strong>stubs</strong>(stubbed_methods_vs_return_values)  &#x21d2; <tt><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
        <div class="docstring">
  <div class="discussion">
    

  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>stubbed_methods_vs_return_values</span>
      
      
        <span class='type'>(<tt>Hash</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if <span class='object_link'><a href="#stubs-instance_method" title="Mocha::Mock#stubs (method)">#stubs</a></span> were called multiple times.</p>
</div>
      
    </li>
  
</ul>


</div>
      </li>
    
  </ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>last-built expectation which can be further modified by methods on <span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span>.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


154
155
156
157
158
159
160
161
162
163
164
165
166</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/mock.rb', line 154</span>

<span class='kw'>def</span> <span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='id identifier rubyid_method_name_or_hash'>method_name_or_hash</span><span class='comma'>,</span> <span class='id identifier rubyid_backtrace'>backtrace</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_expectation'>expectation</span> <span class='op'>=</span> <span class='kw'>nil</span>
  <span class='id identifier rubyid_iterator'>iterator</span> <span class='op'>=</span> <span class='const'>ArgumentIterator</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_method_name_or_hash'>method_name_or_hash</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_iterator'>iterator</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_method_name'>method_name</span><span class='comma'>,</span> <span class='op'>*</span><span class='id identifier rubyid_args'>args</span><span class='op'>|</span>
    <span class='id identifier rubyid_ensure_method_not_already_defined'>ensure_method_not_already_defined</span><span class='lparen'>(</span><span class='id identifier rubyid_method_name'>method_name</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_expectation'>expectation</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="Expectation.html" title="Mocha::Expectation (class)">Expectation</a></span></span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='kw'>self</span><span class='comma'>,</span> <span class='id identifier rubyid_method_name'>method_name</span><span class='comma'>,</span> <span class='id identifier rubyid_backtrace'>backtrace</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_expectation'>expectation</span><span class='period'>.</span><span class='id identifier rubyid_at_least'>at_least</span><span class='lparen'>(</span><span class='int'>0</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_expectation'>expectation</span><span class='period'>.</span><span class='id identifier rubyid_in_sequence'>in_sequence</span><span class='lparen'>(</span><span class='ivar'>@mockery</span><span class='period'>.</span><span class='id identifier rubyid_sequences'>sequences</span><span class='period'>.</span><span class='id identifier rubyid_last'>last</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='ivar'>@mockery</span><span class='period'>.</span><span class='id identifier rubyid_sequences'>sequences</span><span class='period'>.</span><span class='id identifier rubyid_any?'>any?</span>
    <span class='id identifier rubyid_expectation'>expectation</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='id identifier rubyid_args'>args</span><span class='period'>.</span><span class='id identifier rubyid_shift'>shift</span><span class='rparen'>)</span> <span class='kw'>unless</span> <span class='id identifier rubyid_args'>args</span><span class='period'>.</span><span class='id identifier rubyid_empty?'>empty?</span>
    <span class='ivar'>@expectations</span><span class='period'>.</span><span class='id identifier rubyid_add'>add</span><span class='lparen'>(</span><span class='id identifier rubyid_expectation'>expectation</span><span class='rparen'>)</span>
  <span class='kw'>end</span>
  <span class='id identifier rubyid_expectation'>expectation</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="unstub-instance_method">
  
    #<strong>unstub</strong>(*method_names)  &#x21d2; <tt><span class='object_link'>Object</span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Removes the specified stubbed methods (added by calls to <span class='object_link'><a href="#expects-instance_method" title="Mocha::Mock#expects (method)">#expects</a></span> or <span class='object_link'><a href="#stubs-instance_method" title="Mocha::Mock#stubs (method)">#stubs</a></span>) and all expectations associated with them.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Invoking an unstubbed method causes error to be raised</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>mock</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:stubbed_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='symbol'>:result_one</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubbed_method'>stubbed_method</span> <span class='comment'># =&gt; :result_one
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_unstub'>unstub</span><span class='lparen'>(</span><span class='symbol'>:stubbed_method</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubbed_method'>stubbed_method</span> <span class='comment'># =&gt; unexpected invocation: #&lt;Mock:mock&gt;.stubbed_method()</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Unstubbing multiple methods.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_multiplier'>multiplier</span><span class='period'>.</span><span class='id identifier rubyid_unstub'>unstub</span><span class='lparen'>(</span><span class='symbol'>:double</span><span class='comma'>,</span> <span class='symbol'>:triple</span><span class='rparen'>)</span>

<span class='comment'># exactly equivalent to
</span>
<span class='id identifier rubyid_multiplier'>multiplier</span><span class='period'>.</span><span class='id identifier rubyid_unstub'>unstub</span><span class='lparen'>(</span><span class='symbol'>:double</span><span class='rparen'>)</span>
<span class='id identifier rubyid_multiplier'>multiplier</span><span class='period'>.</span><span class='id identifier rubyid_unstub'>unstub</span><span class='lparen'>(</span><span class='symbol'>:triple</span><span class='rparen'>)</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>method_names</span>
      
      
        <span class='type'>(<tt>Array&lt;Symbol&gt;</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>names of methods to unstub.</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


186
187
188
189
190</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/mock.rb', line 186</span>

<span class='kw'>def</span> <span class='id identifier rubyid_unstub'>unstub</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_method_names'>method_names</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_method_names'>method_names</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_method_name'>method_name</span><span class='op'>|</span>
    <span class='ivar'>@expectations</span><span class='period'>.</span><span class='id identifier rubyid_remove_all_matching_method'>remove_all_matching_method</span><span class='lparen'>(</span><span class='id identifier rubyid_method_name'>method_name</span><span class='rparen'>)</span>
  <span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

</div>

      <div id="footer">
  Generated on Tue Nov 11 14:27:03 2025 by
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
  0.9.37 (ruby-3.3.0).
</div>

    </div>
  </body>
</html>